﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActivitiesControl.Domain.Model;
using ActivitiesControl.Domain.Utils;

namespace ActivitiesControl.Domain.Services.Impls
{
	[ExceptionMessageHandler]
	internal class ActivityServices
		: BaseServices, IActivityServices
	{
		private ActivityHandler handler;
		
		public override event MessageHandler MessageRaised = delegate(object sender, MessageEventArgs e) { };

		internal override void RaiseMessage(object sender, MessageEventArgs e)
		{
			MessageRaised(sender, e);
		}

		public ActivityServices()
		{
			handler = new ActivityHandler(ActivityRepository);
		}

		public IEnumerable<string> GetActivityTypes(int customerId)
		{
			return ActivityRepository.GetActivityTypes(customerId);
		}

		public IEnumerable<Activity> GetActivitiesByMonth(int month)
		{
			IList<Activity> list = new List<Activity>();
			var result = ActivityRepository.GetActivitiesByMonth(month);

			foreach (var activity in result)
			{
				activity.Project = ProjectRepository.GetById(activity.ProjectId);
				activity.Resource = ResourceRepository.GetById(activity.ResourceId);
				list.Add(activity);
			}

			MessageRaised(this, new MessageEventArgs { Message = new Message("Atividades", string.Format("{0} atividades retornadas.", list.Count), MessageType.Ok) });

			return list.OrderBy(a => a.ResourceId).OrderBy(a => a.Date.Day);
		}

		public Activity GetActivityById(int id)
		{
			return ActivityRepository.GetById(id);
		}

		public void RegisterActivity(string description, string activityType, int effortHours, Date date, int projectId, int resourceId)
		{
			handler.Register(description, activityType, effortHours, date, projectId, resourceId);
			MessageRaised(this, new MessageEventArgs { Message = new Message("Atividades", "Atividade registrada.", MessageType.Ok) });
		}

		public void UpdateActivity(int activityId, string description, string activityType, int effortHours, Date date, int projectId, int resourceId)
		{
			handler.Update(activityId, description, activityType, effortHours, date, projectId, resourceId);
			MessageRaised(this, new MessageEventArgs { Message = new Message("Atividades", "Atividade atualizada.", MessageType.Ok) });
		}

		public void ExcludeActivity(int activityId)
		{
			handler.Exclude(activityId);
			MessageRaised(this, new MessageEventArgs { Message = new Message("Atividades", "Atividade excluída.", MessageType.Ok) });
		}

        public IEnumerable<Activity> GetActivities(int resourceId, Date begin, Date end, int customerId, int projectId)
        {
            IList<Activity> list = new List<Activity>();

            var result = ActivityRepository.GetActivities(resourceId, begin, end, customerId, projectId);                

            foreach (var activity in result)
            {
                activity.Project = ProjectRepository.GetById(activity.ProjectId);
                activity.Resource = ResourceRepository.GetById(activity.ResourceId);
                list.Add(activity);
            }

			MessageRaised(this, new MessageEventArgs { Message = new Message("Atividades", string.Format("{0} atividades retornadas.", list.Count), MessageType.Ok) });

            return list.OrderBy(a => a.ResourceId).OrderByDescending(a => a.Date.DateTime);
        }
	}
}